
#include "pixeltype.h"

using namespace WtfGraphics;

#define PX_DEFAULT_CONVERSION(pSrc) \
	pThis = new tThis(size = pSrc->getSize()); \
	for(i = 0; i < size; i++) { \
		(*pThis)[i] = (*pSrc)[i]; \
	};

template <typename tElem, int Dim, int iType>
typename PixelBuffer<tElem, Dim, iType>::tRef PixelBuffer<tElem, Dim, iType>::UpcastRef(const IPixelBuffer::tRef &rPxBuf) {
	IPixelBuffer * pPxBuf = &*rPxBuf;

	int i, size;

	tThis * pThis;
	PixelType::tRGBA8 *		pRGBA8;
	PixelType::tRGBA32f *	pRGBA32f;
	
	if(pThis = dynamic_cast<tThis *>(pPxBuf)) {
		// No conversion required, yay!
	} else if (pRGBA8 = dynamic_cast<PixelType::tRGBA8 *>(pPxBuf)) {
		PX_DEFAULT_CONVERSION(pRGBA8);
	} else if (pRGBA32f = dynamic_cast<PixelType::tRGBA32f *>(pPxBuf)) {
		PX_DEFAULT_CONVERSION(pRGBA32f);
	} else {
		throw new std::logic_error("Unknown pixel type");
	};

	return pThis;
};

